/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：呼叫中心-请求处理层-工作台
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taiping.cc.platform.webapi.controller;

import com.taiping.cc.platform.webapi.handler.IndexHandler;
import com.taiping.cc.platform.webapi.vo.ArticleVO;
import com.taipingframework.boot.web.response.result.ApiResult;
import com.taipingframework.boot.web.response.status.ApiStatusEnum;
import com.taipingframework.utility.exception.ApplicationException;
import com.taipingframework.utility.thread.wrapper.BenchmarksFuture;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/index")
@RequiredArgsConstructor
public class IndexController {

    private final IndexHandler indexHandler;

    /**
     * 查询工作台数据
     */
    @GetMapping("/list/data")
    public CompletableFuture<ApiResult<TreeMap<String, String>>> queryForActionDataList() {
        return BenchmarksFuture.completedFuture(indexHandler.queryForActionDataList())
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                }).toCompletableFuture();
    }

    /**
     * 分布式全局唯一ID生成
     */
    @GetMapping("/list/unid")
    public CompletableFuture<ApiResult<ArrayList<String>>> listUnid() {
        return BenchmarksFuture.completedFuture(indexHandler.listUnid())
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                }).toCompletableFuture();
    }

    /**
     * 分布式锁解决方案 - redis
     */
    @GetMapping("/lock/redis")
    public CompletableFuture<ApiResult<Boolean>> redisLock() {
        return BenchmarksFuture.completedFuture(indexHandler.redisLock())
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                }).toCompletableFuture();
    }

    /**
     * 分布式锁解决方案 - zookeeper
     */
    @GetMapping("/lock/zk")
    public CompletableFuture<ApiResult<Boolean>> zookeeperLock() {
        return BenchmarksFuture.completedFuture(indexHandler.zkLock())
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                }).toCompletableFuture();
    }

    /**
     * 文件上传
     */
    @PostMapping("/file/upload")
    public CompletableFuture<ApiResult<HashMap<String, String>>> uploadAttachment(@RequestParam("test") String test,
                                                                                  @RequestParam("file") MultipartFile file) {
        return BenchmarksFuture.completedFuture(indexHandler.uploadAttachment(test, file))
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                }).toCompletableFuture();
    }

    /**
     * 文件下载
     */
    @GetMapping("/file/download/{object_name}")
    public CompletableFuture<Void> downloadAttachment(@PathVariable("object_name") String objectName) {
        return BenchmarksFuture.runAsync(() -> indexHandler.downloadAttachment(objectName))
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                }).toCompletableFuture();
    }

    /**
     * 给前端测试发送get请求
     */
    @GetMapping("/test/get")
    public CompletableFuture<ApiResult<String>> testGetRequest(@RequestParam("un") String timestamp) {
        return BenchmarksFuture.completedFuture(ApiResult.<String>build()
                .setStatus(ApiStatusEnum.SUCCESS)
                .setResult(timestamp))
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                }).toCompletableFuture();
    }

    /**
     * 给前端测试发送post请求
     */
    @PostMapping("/test/post")
    public CompletableFuture<ApiResult<String>> testPostRequest(@RequestBody String requestBody) {
        return BenchmarksFuture.completedFuture(ApiResult.<String>build()
                .setStatus(ApiStatusEnum.SUCCESS)
                .setResult(requestBody))
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                }).toCompletableFuture();
    }

    /**
     * 获取文章列表
     */
    @GetMapping("/table/list")
    public CompletableFuture<ApiResult<ArticleVO>> tableList(@RequestBody String requestBody) {
        return CompletableFuture.completedFuture(indexHandler.getTableList(requestBody))
                .exceptionally(throwable -> {
                    throw new ApplicationException(throwable.getCause());
                }).toCompletableFuture();
    }

}
